﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormDealersBanks : WebFormBase
    {
        public Guid? Dealer_ID
        {
            get
            {
                return string.IsNullOrEmpty(this.Request[BLDealer.PropertiesNames.Dealer_ID]) ? (Guid?)null : new Guid(this.Request[BLDealer.PropertiesNames.Dealer_ID]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                BLDealer dealer = new BLDealer();
                dealer.Dealer_ID = this.Dealer_ID;
                dealer.LoadByIdentifiers();
                this.PageTitle ="بنوك /"+ dealer.Dealer_NameAr;
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getDealersBanks();
            this.RadGridResults.Rebind();
        }
        protected void RadComboBoxBanksClassifications_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.getDealersBanks();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getDealersBanks();
        }
        
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //e.Canceled = true;
                //System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                //newValues[BLAddresse.PropertiesNames.Address_NameAr] = "***";
                //e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLDealer_Bank Dealer_Bank = new BLDealer_Bank();
            if (newValues[BLDealer_Bank.PropertiesNames.AcountNumber] != null)
            {
                Dealer_Bank.AcountNumber = newValues[BLDealer_Bank.PropertiesNames.AcountNumber].ToString();
            }

            Dealer_Bank.Dealer_ID = this.Dealer_ID;
            RadComboBox RadComboBoxBanks = (RadComboBox)editedItem.FindControl("RadComboBoxBanks");
            if (RadComboBoxBanks.SelectedValue != "")
            {
                Dealer_Bank.Bank_ID = new Guid(RadComboBoxBanks.SelectedValue);
            }
            Dealer_Bank.AutoSave();
            if (Dealer_Bank.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLDealer_Bank Dealer_Bank = new BLDealer_Bank();
            if (newValues[BLDealer_Bank.PropertiesNames.AcountNumber] != null)
            {
                Dealer_Bank.AcountNumber = newValues[BLDealer_Bank.PropertiesNames.AcountNumber].ToString();
            }
            Dealer_Bank.ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLDealer_Bank.PropertiesNames.ID].ToString());
            Dealer_Bank.LoadByIdentifiers();
            Dealer_Bank.Dealer_ID = this.Dealer_ID;
            RadComboBox RadComboBoxBanks = (RadComboBox)editedItem.FindControl("RadComboBoxBanks");
            if (RadComboBoxBanks.SelectedValue != "")
            {
                Dealer_Bank.Bank_ID = new Guid(RadComboBoxBanks.SelectedValue);
            }
            
            Dealer_Bank.AutoSave();
            if (Dealer_Bank.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLDealer_Bank Dealer_Bank = new BLDealer_Bank();
            Dealer_Bank.ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLDealer_Bank.PropertiesNames.ID].ToString());
            Dealer_Bank.DeleteByIdentifiers();
            if (Dealer_Bank.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //combobox Banks
                RadComboBox RadComboBoxBanks = (RadComboBox)e.Item.FindControl("RadComboBoxBanks");
                BLBankCollection BankCollection = new BLBankCollection(CashVariables.BankCollection);
                for (int i = 0; i < BankCollection.Count; i++)
                {
                    int index = CashVariables.BankCollection.FindByIentifier(BankCollection[i].Bank_ID);
                    if (index != -1)
                    {
                        BankCollection[i].Bank_NameAr = CashVariables.BankCollection[index].Bank_NameAr;
                    }
                }
                RadComboBoxBanks.DataSource = BankCollection;
                RadComboBoxBanks.DataTextField = BLBank.PropertiesNames.Bank_NameAr;
                RadComboBoxBanks.DataValueField = BLBank.PropertiesNames.Bank_ID;
                RadComboBoxBanks.DataBind();
                if (e.Item.ItemIndex != -1)
                {
                    BLDealer_Bank Dealer_Bank = (BLDealer_Bank)e.Item.DataItem;
                    RadComboBoxBanks.SelectedValue = Dealer_Bank.Bank_ID.ToString();
                }
            }
        }


        private void getDealersBanks()
        {
            BLDealer_BankCollection Dealer_BankCollection = new BLDealer_BankCollection();
            //dealer id
            Dealer_BankCollection.Template.AddFilter(LogicalOperators.And, BLDealer_Bank.PropertiesNames.Dealer_ID, FilterOperators.Equal, this.Dealer_ID, typeof(Guid));
            //load
            Dealer_BankCollection.Load();
            
            for (int i = 0; i < Dealer_BankCollection.Count; i++)
            {
                int index = CashVariables.BankCollection.FindByIentifier(Dealer_BankCollection[i].Bank_ID);
                if (index != -1)
                {
                    Dealer_BankCollection[i].Bank_NameAr = CashVariables.BankCollection[index].Bank_NameAr;
                }
            }

            //serch filter with linq
            Dealer_BankCollection=new BLDealer_BankCollection (from i in Dealer_BankCollection
                                                               where StringHelper.ToUniqueStatement(i.Bank_NameAr+i.AcountNumber).Contains(StringHelper.ToUniqueStatement(this.RadTextBoxSearch.Text))
                                                               select i);
            
            this.RadGridResults.DataSource = Dealer_BankCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLDealer_Bank.PropertiesNames.Identifiers;
        }

    }
}